package com.jams.goodox.activity.teacher;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.qa.entity.Question;
import com.jams.goodox.activity.qa.teacher.TeacherAnswerActivity;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.TimeUtils;
import com.jams.goodox.utils.ToastUtil;

import org.json.JSONObject;

import butterknife.Bind;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by HP on 2018/8/9.
 * 教师问题详情
 */

public class TeacherQuestionAnswerDetailActivity extends BaseActivity {
    private static final String TAG = TeacherQuestionAnswerDetailActivity.class.getSimpleName();

    private int questionId;
    private boolean isAnswer;
    private Question question;

    @Bind(R.id.image)
    SimpleDraweeView image;

    @Bind(R.id.nickname)
    TextView nickname;

    @Bind(R.id.descr)
    TextView descr;

    @Bind(R.id.time)
    TextView time;

    @Bind({R.id.titlebar})
    TextView titlebar;

    @Bind(R.id.price)
    EditText price;

    @Bind(R.id.answer_layout)
    LinearLayout answer_layout;

    @Bind(R.id.answerdetail)
    TextView answerdetail;

    @Bind(R.id.go_answer)
    TextView go_answer;

    private int  yangCoin;

    @Override
    protected int getLayoutId() {
        return R.layout.teacherandetail_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "问题详情");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
//        questionId = getIntent().getIntExtra("questionId",-1);
//        isAnswer = getIntent().getBooleanExtra("isAnswer",false);
        question = (Question) getIntent().getSerializableExtra("question");
        questionId = question.getId();
        isAnswer = question.getQueState() == 0 ? false : true;

        image.setImageURI(question.getImgPath());
        nickname.setText(question.getNickName());
        time.setText(TimeUtils.getDateToString(question.getCreateTime()));
        descr.setText(question.getDescr());

        go_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(TextUtils.isEmpty(price.getText())){
                    ToastUtil.show("请输入价格");
                    return;
                }

                String priceTx = price.getText().toString();
                Log.d(TAG,"priceTx = " +  priceTx.length());

                if(priceTx.length() > 3 || Integer.valueOf(priceTx) >= 15){
                    ToastUtil.show("当前输入价格不能超过15氧币");
                    return;
                }

                Intent intent = new Intent(
                        TeacherQuestionAnswerDetailActivity.this,TeacherAnswerActivity.class);
                intent.putExtra("questionId",questionId);
                intent.putExtra("price",priceTx);
                startActivityForResult(intent,10);
            }
        });

        refreshUI();

        if(isAnswer){
            showAnswer();
        }
    }


    private void refreshUI(){
        if(isAnswer){
            go_answer.setVisibility(View.GONE);
            answer_layout.setVisibility(View.VISIBLE);
            price.setText(yangCoin + "");
            titlebar.setText("查看答案所需氧币");
            price.setFocusable(false);
            price.setEnabled(false);

        }else {
            go_answer.setVisibility(View.VISIBLE);
            answer_layout.setVisibility(View.GONE);
            titlebar.setText("请设置查看答案所需氧币");
            price.setFocusable(true);
            price.setEnabled(true);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 10 && resultCode == RESULT_OK){
            isAnswer = true;
            showAnswer();
        }

        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    //获取问题回答
    private void showAnswer() {
        clearMap();

        params.put("requestData", "{" + "questionId:" + questionId + "}");
        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),strEntity);

        showLoading();
        Call<ResponseBody> example = createCallService(LoginService.class).showAnswer(body);

        example.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                misLoading();
                try {
                    String data = response.body().string();
                    Log.d(TAG, "showAnswer data = " + data);

                    JSONObject jsonObject = new JSONObject(data);

                    if(200 == jsonObject.getInt("code")){
                        JSONObject object = new JSONObject(jsonObject.getString("responseData"));

                        answerdetail.setText(object.getString("content"));
                        yangCoin = object.getInt("yangCoin");
                        refreshUI();

                    }else {
                        if(jsonObject.has("error")){
                            ToastUtil.show("error:" + jsonObject.getString("error") );
                        }else {
                            ToastUtil.show("网络异常！" );
                        }
                    }
                } catch (Exception e) {
                    ToastUtil.show("Exception:" + e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                Log.d(TAG,"onFailure = " + t.getMessage());
            }
        });
    }

}
